package com.example.exerciseintelligenttransportation;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;


public class RegisterActivity extends AppCompatActivity {

    private TextView textView;
    private EditText edtTxtUsers;
    private EditText edtTxtPwd;
    private CheckBox chkPwd;
    private CheckBox chkLogin;
    private Button btnMainRegister;
    private SharedPreferences sp;

    @Override
    protected void onResume() {
        super.onResume();
        boolean isSavePwd = sp.getBoolean("isSavePwd", false);
        chkPwd.setChecked(isSavePwd);
        boolean isAutoRegister = sp.getBoolean("isAutoRegister", false);
        chkLogin.setChecked(isAutoRegister);
        String userName = sp.getString("userName", "");
        String userPwd = sp.getString("userPwd", "");
        if (isSavePwd) {
            edtTxtUsers.setText(userName);
            edtTxtPwd.setText(userPwd);
        }

        if (isAutoRegister) {
            if (!userName.equals("") & !userPwd.equals("")) {
                btnMainRegister.performClick();
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();
        chkPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences.Editor editor=sp.edit();
                editor.putBoolean("isSavePwd",isChecked);
                editor.commit();
            }
        });
        chkLogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences.Editor editor=sp.edit();
                editor.putBoolean("isSavePwd",isChecked);
                editor.putBoolean("isAutoLogin",isChecked);
                editor.commit();
            }
        });
        sp=getSharedPreferences("RegisterSetting",MODE_PRIVATE);
        btnMainRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userNmae = edtTxtUsers.getText().toString().trim();
                String userPwd = edtTxtPwd.getText().toString().trim();
//                HttpUtil.asyncRequest(new HttpUtil.AsynCallBack() {
//
//                    @Override
//                    public void onResponse(String json) {
//                        RegisterBean registerBean = new Gson().fromJson(json,RegisterBean.class);
//                        if (RegisterBean.getRESULT().equals("S")){
//                            SharedPreferences.Editor editor = sp.edit();
//                            if (chkPwd.isChecked()){
//                                editor.putString("userName",userNmae);
//                                editor.putString("userPwd",userPwd);
//                            }else {
//                                editor.putString("userName",null);
//                                editor.putString("userPwd",null);
//                            }
//                            editor.commit();
//                            Intent intent=new Intent(getApplicationContext(),MainActivity.class);
//                            startActivity(intent);
//                            return;
//                        }else if (RegisterBean.getRESULT().equals("F")){
//                            Toast.makeText(RegisterActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
//                        }
//
//                    }
//                    @Override
//                    public void onFailure(Exception e) {
//                        Log.i("Register",e.getMessage());
//                    }
//                },"user_Register",new String[]{"UserName","UserPwd"},userNmae,userPwd);

                }


        });

    }

    private void initView() {
        textView = (TextView) findViewById(R.id.textView);
        edtTxtUsers = (EditText) findViewById(R.id.edtTxt_users);
        edtTxtPwd = (EditText) findViewById(R.id.edtTxt_pwd);
        chkPwd = (CheckBox) findViewById(R.id.chk_pwd);
        chkLogin = (CheckBox) findViewById(R.id.chk_login);
        btnMainRegister = (Button) findViewById(R.id.btn_main_register);
    }
}